var router = require('express').Router();
var util = require('util');
var uuid = require('node-uuid');
var cookie = require('cookie');
var EventProxy = require('eventproxy');

var Consts = require('../consts');
var resultJson = require('../result-json');
var Utils = require('../utils');
var logger = require('../models/logger');
var Project = require('../models/Project');
var Course = require('../models/Course');
var nodeExcel = require('../xls-export');
var Teacher = require('../models/Teacher');

router.post('/', function(req, res) {
	res.send('respond with a resource');
});

router.post('/list', function(req, res) {
	var project = req.body;
	var proxy = new EventProxy();
	proxy.all("results", "count", function(results, count) {
		res.send(resultJson(0, {
			results: results,
			count: count
		}))
	})
	var start = project.start || 0;
	var count = project.count || Consts.MAX;
	var item = {};

	if (req.body.owner)
		item.owner = req.body.owner;
	if (req.body.status)
		item.status = req.body.status;

	Project.find(item, {
		children: 0,
		__v: 0
	}).sort({
		status: 1
	}).skip(start).limit(count).exec(function(err, projects) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		proxy.emit("results", projects);
	});
	Project.count(item, function(err, num) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		proxy.emit("count", num);
	});
});

//改变状态
router.post("/chengeStatus", function(req, res) {
	var status = req.body.status;
	var _id = req.body._id;
	var rejectDes = req.body.rejectDes;
	Project.update({
		_id: _id
	}, {
		$set: {
			status: status,
			rejectDes: rejectDes
		}
	}).exec(function(err) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		res.send(resultJson(0));
	});
});

router.post('/upsert', function(req, res) {
	var project = req.body;
	project = JSON.stringify(project);
	project = project.replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/</g, '&lt;').replace(/>/g, '&lg;');
	project = JSON.parse(project);
	if (!project)
		return res.send(resultJson(3));
	console.log(project);
	if (!project.children)
		project.children = [];
	if (project._id) {
		if (!project.course)
			project.course = [];
		Project.update({
			_id: project._id
		}, project, function(err) {
			if (err)
				return res.send(resultJson(2));
			return res.send(resultJson(0));
		});
	} else {
		project.create = new Date();
		project.status = Consts.STATUS.UNCOMMIT;
		//名字唯一性判断
		Project.count({
			name: project.name
		}, function(err, cnt) {
			if (err)
				return res.send(resultJson(2));
			if (cnt)
				return res.send(resultJson(403));
			var newProject = new Project(project);
			newProject.save(function(err) {
				if (err)
					return res.send(resultJson(2));
				return res.send(resultJson(0, newProject));
			});
		});
	}
});

router.post('/remove', function(req, res) {
	var _id = req.body._id;
	if (!_id)
		return res.send(resultJson(3));
	Direction.count({
		"projects.id": _id
	}).exec(function(err, num) {
		if (num > 0)
			return res.send(resultJson(110));
		Project.remove({
			_id: _id
		}, function(err) {
			if (err)
				return res.send(resultJson(2));
			return res.send(resultJson(0));
		});
	});

});


router.post('/detail', function(req, res) {
	var _id = req.body._id;
	if (!_id)
		return res.send(resultJson(3));

	Project.findById({
		_id: _id
	}, function(err, project) {
		if (err)
			return res.send(resultJson(2));

		var project = JSON.parse(JSON.stringify(project));

		Course.find({
				_id: {
					$in: project.course || []
				}
			})
			.select("_id name desc photo")
			.exec(function(err, courses) {
				if (err)
					logger.error(err);

				console.log(project.course, courses);
				project.course = [];
				project.course = courses;
				Teacher.find({
					_id: {
						$in: project.teachers || []
					}
				}).exec(function(err, teachers) {
					if (err || !teachers)
						teachers = [{}]
					project.teachers = teachers;
					return res.send(resultJson(0, project));

				});
			});
	});
});

//根据项目id推荐和取消推荐
router.post("/recommend", function(req, res) {
	var _id = req.body._id;
	var recommend = req.body.recommend;
	console.log("id:" + _id + "   recommend:" + recommend)
	if (!_id || !recommend)
		return res.send(resultJson(3));

	var query;
	if (recommend == "true")
		query = Project.update({
			_id: _id
		}, {
			$set: {
				recommend: new Date()
			}
		});
	else
		query = Project.update({
			_id: _id
		}, {
			$unset: {
				recommend: 0
			}
		});

	query.exec(function(err) {
		if (err) {
			logger.error(err);
			res.send(resultJson(2));
		} else
			res.send(resultJson(0));
	});
});
module.exports = router;